package org.ehe.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.system.domain.HrEmployeeFamily;
import org.ehe.system.domain.bo.HrEmployeeFamilyBo;
import org.ehe.system.domain.vo.HrEmployeeFamilyVo;
import org.ehe.system.mapper.HrEmployeeFamilyMapper;
import org.ehe.system.service.IHrEmployeeFamilyService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 员工家庭成员Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class HrEmployeeFamilyServiceImpl implements IHrEmployeeFamilyService {

    private final HrEmployeeFamilyMapper baseMapper;
    private final Converter converter;

    /**
     * 查询员工家庭成员
     */
    @Override
    public HrEmployeeFamilyVo queryById(Long familyId) {
        return baseMapper.selectVoById(familyId);
    }

    /**
     * 根据工号查询员工家庭成员列表
     */
    @Override
    public List<HrEmployeeFamilyVo> queryListByEmployeeNo(String employeeNo) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<HrEmployeeFamily>()
                .eq(HrEmployeeFamily::getEmployeeNo, employeeNo)
                .orderByDesc(HrEmployeeFamily::getIsEmergencyContact)
                .orderByDesc(HrEmployeeFamily::getCreateTime));
    }

    /**
     * 查询员工家庭成员列表
     */
    @Override
    public TableDataInfo<HrEmployeeFamilyVo> queryPageList(HrEmployeeFamilyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HrEmployeeFamily> lqw = buildQueryWrapper(bo);
        //lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNo()),HrEmployeeFamily::getEmployeeNo,bo.getEmployeeNo());
        Page<HrEmployeeFamilyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询员工家庭成员列表
     */
    @Override
    public List<HrEmployeeFamilyVo> queryList(HrEmployeeFamilyBo bo) {
        LambdaQueryWrapper<HrEmployeeFamily> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HrEmployeeFamily> buildQueryWrapper(HrEmployeeFamilyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HrEmployeeFamily> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEmployeeNo()), HrEmployeeFamily::getEmployeeNo, bo.getEmployeeNo());
        lqw.like(StringUtils.isNotBlank(bo.getMemberName()), HrEmployeeFamily::getMemberName, bo.getMemberName());
        lqw.eq(StringUtils.isNotBlank(bo.getRelationship()), HrEmployeeFamily::getRelationship, bo.getRelationship());
        lqw.eq(StringUtils.isNotBlank(bo.getIsEmergencyContact()), HrEmployeeFamily::getIsEmergencyContact,
                bo.getIsEmergencyContact());
        lqw.orderByDesc(HrEmployeeFamily::getCreateTime);
        return lqw;
    }

    /**
     * 新增员工家庭成员
     */
    @Override
    public Boolean insertByBo(HrEmployeeFamilyBo bo) {
        HrEmployeeFamily add = converter.convert(bo, HrEmployeeFamily.class);
        // 标记为已加密
        add.setEmployeeId(bo.getEmployeeId());
        add.setIsEncrypted("1");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFamilyId(add.getFamilyId());
        }
        return flag;
    }

    /**
     * 修改员工家庭成员
     */
    @Override
    public Boolean updateByBo(HrEmployeeFamilyBo bo) {
        HrEmployeeFamily update = converter.convert(bo, HrEmployeeFamily.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除员工家庭成员
     */
    @Override
    public Boolean deleteByIds(Collection<Long> familyIds) {
        return baseMapper.deleteByIds(familyIds) > 0;
    }

}
